using Common.MangoDB;
using MongoDB.Driver;

namespace Common.MongoDB.Test;

[TestClass]
public class MongoDbHelperTest
{
  [TestMethod]
  public void TestDeleteDocument()
  {
    var helper = new MongoDbHelper("your_connection_string", "testdb");
    var testDoc = new TestDocument { Name = "To Be Deleted" };

    // 插入文档
    helper.Insert("testcollection", testDoc);

    // 删除文档
    helper.Delete<TestDocument>("testcollection", testDoc.Id.ToString());

    // 尝试获取已删除的文档
    var result = helper.GetDocumentById<TestDocument>("testcollection", testDoc.Id.ToString());

    Assert.IsNull(result);
  }

  [TestMethod]
  public void TestUpdateDocument()
  {
    var helper = new MongoDbHelper("your_connection_string", "testdb");
    var testDoc = new TestDocument { Name = "Original Name" };

    // 插入文档
    helper.Insert("testcollection", testDoc);

    // 更新文档
    var filter = Builders<TestDocument>.Filter.Eq("_id", testDoc.Id);
    var update = Builders<TestDocument>.Update.Set("Name", "Updated Name");
    helper.Update("testcollection", filter, update);

    // 获取更新后的文档
    var updatedDoc = helper.GetDocumentById<TestDocument>("testcollection", testDoc.Id.ToString());

    Assert.IsNotNull(updatedDoc);
    Assert.AreEqual("Updated Name", updatedDoc.Name);
  }

  [TestMethod]
  public void TestGetAllDocuments()
  {
    var helper = new MongoDbHelper("your_connection_string", "testdb");

    // 获取所有文档
    var documents = helper.GetAllDocuments<TestDocument>("testcollection");

    Assert.IsNotNull(documents);
    Assert.IsTrue(documents.Count > 0);
  }

  [TestMethod]
  public void TestInsertAndGetDocumentById()
  {
    var helper = new MongoDbHelper("your_connection_string", "testdb");
    var testDoc = new TestDocument { Name = "Test Document" };

    // 插入文档
    helper.Insert("testcollection", testDoc);

    // 获取插入的文档
    var result = helper.GetDocumentById<TestDocument>("testcollection", testDoc.Id.ToString());

    Assert.IsNotNull(result);
    Assert.AreEqual(testDoc.Name, result.Name);
  }
}